package com.fancy.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.fancy.core.model.UserModel;
import com.fancy.core.service.UserService;

@Controller
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Resource
	private UserService userService;
	


	@RequestMapping(value = "/user_add.html", method = RequestMethod.GET)
	public String userAddGet() {
        return "user/add";
	}
	
	@RequestMapping(value = "/user_add.json")
	public void userAddPost(String name, Integer age, Integer sex,Model model) {
		userService.add(name, age, sex);
		model.addAttribute("success", "true");
	}
	
	@RequestMapping(value = "/user_update.html", method = RequestMethod.GET)
	public String userUpdateGet(Integer id, Model model) {
		logger.error("我们在一起");
		
		model.addAttribute("user", userService.get(id));
		return "user/update";
	}
	
	@RequestMapping(value = "/user_update.json", method = RequestMethod.POST)
	public void userUpdatePost(Model model,UserModel userModel) {
		userService.update(userModel);
		model.addAttribute("success", "true");
	}
	@RequestMapping(value = "/user_del.json")
	public void userDel(Integer id, Model model) {
		userService.del(id);
		model.addAttribute("success", "true");
	}
	
	@RequestMapping("/user_list.html")
	public String user_list(Model model, @RequestParam(defaultValue = "6") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageIndex) {
		Integer start = (pageIndex - 1) * pageSize;
		List<UserModel> userList = userService.getList(start, pageSize);
		model.addAttribute("userList", userList);
		model.addAttribute("totalCount", userService.getListCount());
		model.addAttribute("pageSize", pageSize);
		model.addAttribute("pageIndex", pageIndex);
		model.addAttribute("start", start);
		return "user/list";
	}
	
}
